concrete MiniGrammarChi of MiniGrammar = open Prelude, MorphoChi in {
 
  flags coding=utf8;

  lincat
    Phr  = {s : Str} ;
    S    = {s : Str} ;
    QS   = {s : Str} ;
    Cl   = Clause ;
    QCl  = Clause ;
    NP   = NounPhrase ;
    IP   = NounPhrase ;
    CN   = Noun ;
    Det  = {s : Str ; n : Number} ;
    IDet = {s : Str ; n : Number} ;
    AP   = {s : Str ; cop : Bool} ;
    Adv  = Adverb ; 
    AdA  = {s : Str} ;
    VP   = VerbPhrase ;
    N    = Noun ;
    A    = Adjective ;
    V    = Verb ;
    V2   = Verb2 ;
    Conj = Conjunction ;
    Subj = {s,s2 : Str} ;
    Pol  = {s : Str ; p : Bool} ;

  lin
    PhrS  = postfixSS "。" ;
    PhrQS = postfixSS "？" ;

    UseCl = dirCl ;


    --Questions where the question particle ma is added to the end.
    --How about wh-questions? Do they also use ma?
    UseQCl pol qcl =
      let
        cl = dirCl pol qcl
      in
        {s = cl.s ++ questionParticle } ;


    QuestCl cl = cl ;

{- For yes-no (是不是) questions it would be like this:
   TODO: form the question at an early enough stage, that we can separate the verb from complement, subject and adverbs. Needs to be done earlier.
    QuestCl cl = {
      s = cl.s ! questionYesNo -- This doesn't exist, just ignore
    } ;
-}

    -- For sentences like "because...so". If reason is first,
    -- sentence has to have subj.s2 "therefore/so".
    -- Dir: the waiter stinks because he didn't shower
    -- Inv: because the waiter didn't shower, so he stinks
    SubjS subj s = lin Adv {
      s = \\ord => case ord of {
         Dir => subj.s ++ s.s ;
         Inv => subj.s ++ s.s ++ subj.s2 } 
    } ;

    PredVP = predVP ;

    QuestVP ip vp = 
      let
        cl = predVP ip vp
      in 
        {s = \\_ => cl.s ! Dir ;
         compl = vp.compl } ;

    QuestV2 ip np v2 = {
      s = \\ord,pol => 
          let 
            vp : VerbPhrase = predVerb v2
          in 
          bothWays (ip.s ++ (predVP np vp).s ! ord ! pol) v2.c ;
      compl = np.s
    } ;

    ComplV2 v np = insertObject (v.c ++ np.s) (predVerb v) ;

    ComplAP ap = {
      s = \\_,pol,n => {
        fin = posneg (copula ap.cop) pol ; --either copula or degree adverb
        inf = [] } ;
      compl = case ap.cop of {
        True  => ap.s ++ relationParticle ;
        False => ap.s } ; --no copula, no relation particle (的 "de")
      adv   = lin Adv {s = \\_ => [] } ; 
    } ;

    DetCN det cn = 
      let
        dcl = getDet det cn
      in
        {s = dcl ++ cn.s ; n = det.n} ;

    --Adjective as a modifier marked with 的
    ModCN ap cn  = {s = ap.s ++ relationParticle ++ cn.s ; cl = cn.cl} ;

    AdVP adv = insertAdverb adv ;

    AdAP ada ap = {s = ada.s ++ ap.s ; cop = ap.cop} ;

    IDetCN det cn = {s = det.s ++ cn.s ; n = det.n } ;

    ConjS c a b = {s = c.s ++ a.s ++ c.s2 ++ b.s} ;
    ConjNP c a b = {s = c.n ++ a.s ++ c.n2 ++ b.s ; n = Pl} ;

    UseN n = n ;
    UseA a = a ;
    UseV = predVerb ;  

    this_Det = {s = "这" ; n = Sg} ; --zhè
    that_Det = {s = "那" ; n = Sg} ; --nà
    these_Det = {s = "这些" ; n = Pl} ; --zhè
    those_Det = {s = "那些" ; n = Pl} ; --nà
    every_Det = {s = "每" ; n = Sg} ; --měi
    theSg_Det = {s = "the" ; n = Sg} ;
    thePl_Det = {s = "the" ; n = Pl} ;
    indef_Det = {s = "a" ; n = Sg} ;
    plur_Det = {s = [] ; n = Pl} ;
    two_Det = {s = "two" ; n = Pl} ;

    today_Adv = lin Adv { s = \\_ => "今天" } ; 

    very_AdA = {s = "很"} ; --hěn
    which_IDet = {s = "哪" ; n = Sg} ; --nǎ

    --Different conjunctions for nouns, verbs, adjectives and sentences
    and_Conj = {s = "又" ; s2 = "又" ; ---- ?
                a = "又" ; a2 = "又" ; --yòu, yòu
                n = "及" ; n2 = "又" ; --jí, yòu
                v = "边" ; v2 = "边"   --biān, biān
               } ;

    because_Subj = {s = "因为"; s2 = "所以" } ; 

    PPos = {s = [] ; p = True} ;
    PNeg = {s = "不" ; p = False} ; --s field not really needed anywhere

  oper

-- TYPES
    NounPhrase = {s : Str ; n : Number} ;
    VerbPhrase = {s : Order => Bool => Number => {fin,inf : Str} ; 
              compl : Str ; adv : Adverb} ;
    Clause = {s : Order => Bool => Str } ;


-- CLAUSES

    dirCl : Pol -> Cl -> S = \pol,cl ->
      lin S {s = cl.s ! Dir ! pol.p} ;

    --How do we want to invert? Complement can be placed anywhere
    invCl : Pol -> Cl -> S = \pol,cl ->
      lin S {s = cl.s ! Inv ! pol.p } ;

    predVP : NounPhrase -> VerbPhrase -> Clause = \np,vp -> {
      s = \\ord,pol => 
          let vps = vp.s ! ord ! pol ! np.n 
          in case ord of {

            --    N.N.    stinks    /   loves    fish       today/because <S>
            Dir => np.s ++ vps.fin ++ vps.inf ++ vp.compl ++ vp.adv.s ! Dir;
                                          
            --What is an inverted word order in Chinese?
            --today/because <S>, so  N.N.    stinks/loves fish
            Inv => vp.adv.s ! Inv ++ np.s ++ vps.fin ++ vps.inf ++ vp.compl

            -- topic-comment structure : O, S V (adv)
            --Inv => vp.compl ++ np.s ++ vps.fin ++ vps.inf ++ vp.adv.s ! Dir
            } 
    } ;


-- QUESTIONS
-- 2 ways of forming questions: with question particle "ma" at the end of the clause, or by repeating the verb and the negation.
-- Only one way, can't combine both.

    -- Same construction with positive and negative question
    questionParticle : Str = "吗" ;

{-  -- Not used in this grammar, but for future!
    -- 是不是 questions 
    -- When the positive / negative form is used with two-character verbs (like zhī dào), the verb is split after the first character, as in “zhī bù zhī dào.” 
    questionYesNo : Str -> Str = \verb ->
      (take 1 verb) ++ "不" ++ verb ;
-}


--OTHER STUFF

    -- Relation particle 的 "de" has many functions
    -- possession: "wǒ de" = my
    -- after modifier adjective: "piàoliang de rén" = beautiful person
    -- after nouns and stative adjectives as copula complements:
    --                           "wǒ shì piàoliang de" = I am beautiful
    relationParticle : Str = "的" ;

    --stative adjectives and nouns need a copula "shì"
    --verb-like adjectives are without copula, but have degree adverb "hen".
    copula : Bool -> Str = \stative -> case stative of {
      True  => "是" ;
      False => very_AdA.s
    } ;


    --Det : determiner word + classifier (depends on the noun)
    --If det.n is Pl or det.s empty: no classifier
    getDet : Det -> CN -> Str = \det,cn -> 
      let 
        n : Number = det.n ;
        s : Str    = det.s
      in
-- Matching string gives error "Non-exhaustive patterns in function descend"
--        case <n,s> of {
--      <Sg,""> => det.s ;
--      <Sg,_> => det.s ++ cn.cl ;
--      <_,_> => det.s
        case n of {
      Sg => det.s ++ cn.cl ;
      Pl => det.s
    } ;

    posneg : Str -> Bool -> Str = \verb,b -> case b of {
       True  => verb ;       --positive polarity
       False => "lol不" ++ verb --negative polarity
    } ;


    predVerb : Verb -> VerbPhrase = \verb -> {
      s = \\ord,pol,n => 
        let 
          inf = posneg verb.s pol ; --How do Chinese serial verbs work?
          fin = posneg verb.s pol ; --Do we need both fin and inf here?
          aux = []                  --And what should be here?
        in
        case <ord,pol> of {
          <Dir,True> => {fin = []  ; inf = fin } ;
          _          => {fin = aux ; inf = inf }
          } ;
      compl = [] ;
      adv   = lin Adv {s = \\_ => [] } ;

      } ;

    insertObject : Str -> VerbPhrase -> VerbPhrase = 
      \obj,vp -> {
      s = vp.s ;
      compl = obj ;
      adv = vp.adv 
    } ;

    insertAdverb : Adverb -> VerbPhrase -> VerbPhrase = 
      \adv,vp -> {
      s = vp.s ;
      compl = vp.compl ;
      adv = adv
    } ;
}